package phantri.uit.menunhahang;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MonanActivity extends Activity {
	ArrayList<MonanObj> arraylistMonan;
	ArrayAdapter<MonanObj> arrayadapMonan;
	ListView listMonan;
	String strInfo;

	protected Dialog onCreateDialog(int id) {
		Dialog d = null;
		AlertDialog.Builder[] builder = new AlertDialog.Builder[listMonan
				.getCount()];
		builder[id] = new AlertDialog.Builder(this);
		builder[id].setMessage(strInfo).setCancelable(false)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		d = builder[id].create();
		return d;
	}
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.monan);
		arraylistMonan = new ArrayList<MonanObj>();
		listMonan = (ListView) findViewById(R.id.list_monan);
		try {
			GetMonan temp = new GetMonan();
			for (MonanObj monanobj : temp.getMonan()) {
				arraylistMonan.add(monanobj);
			}
			arrayadapMonan = new MenuListAdapter(this, arraylistMonan);
			listMonan.setAdapter(arrayadapMonan);

		} catch (Exception e) {
			e.toString();
		}
	}

	public class MenuListAdapter extends ArrayAdapter<MonanObj> {

		private ArrayList<MonanObj> items;
		private Activity context;

		public MenuListAdapter(Context context, ArrayList<MonanObj> items) {
			super(context, R.layout.listrow, items);
			this.items = items;
			this.context = (Activity) context;
		}

		public class ViewHolder {
			protected TextView text;
			protected TextView gia;
			protected TextView mota;
			protected Button tang;
			protected Button giam;
			protected Button datmon;
			protected Button chitiet;
			protected TextView soluong;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			View v = null;
			if (convertView == null) {
				LayoutInflater inflater = context.getLayoutInflater();
				v = inflater.inflate(R.layout.listrow, null);
				final ViewHolder vh = new ViewHolder();
				vh.text = (TextView) v.findViewById(R.id.listrow_txt);
				vh.gia = (TextView) v.findViewById(R.id.listrow_gia);
				vh.mota = (TextView) v.findViewById(R.id.listrow_mota);
				vh.tang = (Button) v.findViewById(R.id.btn_tang);
				vh.giam = (Button) v.findViewById(R.id.btn_giam);
				vh.datmon = (Button) v.findViewById(R.id.btn_datmon);
				vh.soluong = (TextView) v.findViewById(R.id.txt_soluong);
				vh.chitiet = (Button) v.findViewById(R.id.btn_chitiet);
				v.setTag(vh);
			} else {
				v = convertView;
			}
			final ViewHolder holder = (ViewHolder) v.getTag();
			holder.text.setText("\n" + "--"
					+ items.get(position).getName().toUpperCase() + "--");
			holder.gia.setText("-Giá: "
					+ Integer.parseInt(items.get(position).getPrice())
					+ " VNĐ/phần");
			holder.mota.setText("-Giới thiệu: " + "\n"
					+ items.get(position).getDescription());
			holder.soluong.setText(Integer.toString(items.get(position).getSoluong()));

			holder.tang.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub	
					int soluongtang = Integer.parseInt(holder.soluong.getText().toString());
					if (soluongtang <= 99) {
						holder.tang.setClickable(true);
						soluongtang += 1;
						items.get(position).setSoluong(soluongtang);
						holder.soluong.setText(Integer.toString(items.get(position).getSoluong()));
					} else {
					}
				}
			});
			
			holder.giam.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int soluonggiam = Integer.parseInt((String) holder.soluong
							.getText().toString());
					if (soluonggiam > 0) {
						holder.giam.setClickable(true);
						soluonggiam -= 1;
						items.get(position).setSoluong(soluonggiam);
						holder.soluong.setText(Integer.toString(items.get(position).getSoluong()));
					} else {
					}
				}
			});
			holder.datmon.setOnClickListener(new OnClickListener() {
				int requestCode;

				public void onClick(View v) {
					// TODO Auto-generated method stub
					String info;
					int soluong = Integer.parseInt((String) holder.soluong
							.getText().toString());
					if (soluong != 0) {
						info = "Bạn đã chọn món " + "'"
								+ items.get(position).getName().toUpperCase()
								+ "', với số lượng " + holder.soluong.getText();
						// code o day
						int thanhtien = Integer.parseInt(holder.soluong
								.getText().toString())
								* Integer.parseInt(items.get(position)
										.getPrice());
						DachonRequest dachonreq = new DachonRequest();
						try {
							dachonreq.requestDachon(1, items.get(position).getName().toString(), Integer.parseInt(items.get(position).getPrice()), thanhtien, items.get(position).getSoluong());
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					} else {
						info = "Xin cho biết số lượng món " + "'"
								+ items.get(position).getName().toUpperCase()
								+ "' là bao nhiêu!";
					}
					Toast toast = Toast.makeText(context, info,
							Toast.LENGTH_SHORT);
					toast.show();

				}
			});
			holder.chitiet.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					strInfo = "--"
							+ items.get(position).getName().toUpperCase() + ":"
							+ "\n" + items.get(position).getInfo();
					showDialog(position);
				}
			});

			return v;
		}
	}

}
